package com.baiyinet.ui.main;


import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baiyinet.R;
import com.baiyinet.biz.PageStatus;
import com.baiyinet.ui.BaseToolbarFragment;
import com.baiyinet.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.res.StringRes;
import org.androidannotations.api.BackgroundExecutor;

import static com.baiyinet.ui.general.GeneralActivity.RESULT_RELOAD;

/**
 * 为了同时保持 TabLayout 中每个 WebView 的内容，不得不存在多个相似的 Fragment
 * @author J.Huang
 * @version 1.0
 * @since 16/12/6
 */
@EFragment(R.layout.frag_webview_2)
public class WebViewFragment2 extends BaseToolbarFragment {

    @FragmentArg("pageStatus")
    PageStatus pageStatus;

    @StringRes(R.string.network_disabled)
    protected String strNetworkDisabled;

    @AfterViews
    void init() {
        initData(pageStatus);
    }

    @Override
    protected void findViews() {
        mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_frag_2);
        mToolbarTitle = (TextView) getActivity().findViewById(R.id.tv_frag_toolbar_title_2);
        mToolbarBottomLine = getActivity().findViewById(R.id.view_toolbar_bottom_line_2);
//        mWebView = (WebView) getActivity().findViewById(R.id.webView_frag_2);
        mLayoutWebView = (LinearLayout) getActivity().findViewById(R.id.layout_webview_frag_2);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        mLayoutWebView.addView(mWebView, layoutParams);
        mRLayoutWebView = (RelativeLayout) getActivity().findViewById(R.id.rlayout_webview_frag_2);
        mRLayoutWebView.addView(networkFailedView, layoutParams);
        mToolbarTitle.setText(pageStatus.menuName);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case RESULT_RELOAD:
                if (Utils.Network.isNetworkAvailable()) {
                    mWebViewManager.reload();
                } else {
                    Utils.UI.showToastShort(strNetworkDisabled);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @UiThread
    public void reloadWebView() {
        if (mWebViewManager != null) {
            mWebViewManager.reload();
            mReloaded = true;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        BackgroundExecutor.cancelAll("checkWebView2", true);
    }

    @Background(delay = 500, id = "checkWebView2")
    public void checkWebView() {
        if (!mReloaded &&
                (mToolbarTitle == null
                        || mToolbarTitle.getText() == null
                        || TextUtils.isEmpty(mToolbarTitle.getText().toString()))) {
            reloadWebView();
        }
    }
}
